package Thread;
public class Demo18 {
    //给两个线程加同一把锁，解决线程不安全的问题，这里不在乎加锁的对象，只在乎两线程是否加的是同一把锁
    private static int count=0;
    public static void main(String[] args) throws InterruptedException {
        Object locker=new Object();
        Thread t1=new Thread(()->{
            synchronized (locker){for (int i = 0; i < 5000; i++) {
                count++;
            }
            }

        });
        Thread t2=new Thread(()->{
            synchronized (locker){
                for (int i = 0; i < 5000; i++) {
                    count++;
                }
            }

        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count); //10000
    }
}

